__file__ = 'A19.py'
__author__ = 'Jerry Liu'
__date__ = '2016-04-18'

"""
    问题：将 第 0016 题中的 numbers.xls 文件中的内容写到 numbers.xml 文件中，如下所示：
        <?xml version="1.0" encoding="UTF-8"?>
        <root>
        <numbers>
        <!--
            数字信息
        -->

        [
            [1, 82, 65535],
            [20, 90, 13],
            [26, 809, 1024]
        ]

        </numbers>
        </root>
"""
from openpyxl.reader.excel import load_workbook
from xml.dom.minidom import Document

def getdata(path):
    wb = load_workbook(path)
    # 读取第一个sheet
    snames = wb.get_sheet_names()
    sheet = wb.get_sheet_by_name(snames[0])

    mdata = []
    #读取数据
    for line in range(sheet.get_highest_row()):
        line += 1
        list = []
        list.append(sheet.cell(row=line, column=1).value)
        list.append(sheet.cell(row=line, column=2).value)
        list.append(sheet.cell(row=line, column=3).value)
        mdata.append(list)

    cxml = Document()
    eRoot =cxml.createElement('root')
    cxml.appendChild(eRoot)
    eNums = cxml.createElement('numbers')
    eRoot.appendChild(eNums)
    eComments = cxml.createComment('数字信息')
    eNums.appendChild(eComments)
    eData = cxml.createTextNode(str(mdata))
    eNums.appendChild(eData)

    f = open('d://numbers.xml', 'w')
    cxml.writexml(f)
    f.close()

if __name__ == '__main__':
    getdata('d://numbers.xlsx')
